package main

import (
	"fmt"
	"net"
)

func main() {

	//指定服务器的通讯协议,ip+port
	listener, err := net.Listen("tcp", "127.0.0.1:8000")
	if err != nil {
		fmt.Println("listener error", err)
		return
	}
	defer listener.Close()

	//阻塞监听客户端连接请求
	fmt.Println("服务器等待客户端建立连接:")
	conn, err := listener.Accept()
	defer conn.Close()
	if err != nil {
		fmt.Println("accept error", err)
		return
	}

	for {
		data := make([]byte, 4096)
		n, err := conn.Read(data)
		if err != nil {
			fmt.Println("read error", err)
			return
		}
		fmt.Println("服务器读到数据：", string(data[:n]))

		conn.Write(data[:n])
	}

}
